package org.giovanolli.mumi.service.impl;

import java.util.Calendar;
import java.util.GregorianCalendar;

import org.giovanolli.mumi.dao.Dao;
import org.giovanolli.mumi.dao.PatientDao;
import org.giovanolli.mumi.entities.AttentionTime;
import org.giovanolli.mumi.entities.AttentionTypeMode;
import org.giovanolli.mumi.entities.MovementType;
import org.giovanolli.mumi.entities.Patient;
import org.giovanolli.mumi.entities.filter.MovementFilter;
import org.giovanolli.mumi.service.MovementService;
import org.giovanolli.mumi.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

// TODO: Auto-generated Javadoc
/**
 * The Class PatientServiceImpl.
 */
@Service("patientService")
public class PatientServiceImpl extends AbstractService<Patient> implements PatientService {

	/** The patient dao. */
	@Autowired
	private PatientDao patientDao;

	@Autowired
	private MovementService movementService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.giovanolli.mumi.service.BaseObjectService#getServiceDao()
	 */
	protected Dao<Patient> getServiceDao() {
		return patientDao;
	}

	public Boolean isSolvent(AttentionTime attentionTime) {
		boolean rv = false;
		MovementFilter movementFilter = new MovementFilter();
		movementFilter.setPatient(attentionTime.getPatient());
		movementFilter.setSpecialistFee(attentionTime.getSpecialistFee());

		if (attentionTime.getSpecialistFee().getAttentionTypeMode().equals(AttentionTypeMode.MONTHLY)) {
			Calendar currentDate = new GregorianCalendar();
			int currYear = currentDate.get(Calendar.YEAR);
			int currMonth = currentDate.get(Calendar.MONTH) + 1;

			movementFilter.setMonth(currMonth);
			movementFilter.setYear(currYear);
			movementFilter.setMovementType(MovementType.POSITIVE);
			rv = movementService.getList(movementFilter).size() > 0;
		} else {
			movementFilter.setMovementType(MovementType.POSITIVE);
			Integer positiveAccounts = movementService.getList(movementFilter).size();
			movementFilter.setMovementType(MovementType.NEGATIVE);
			Integer negativeAccounts = movementService.getList(movementFilter).size();
			rv = positiveAccounts - negativeAccounts > 0;
		}
		return rv;
	}

}
